/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.enums.DepartmentStatus;
import com.jiantong.user.enums.DepartmentType;
import com.jiantong.user.impl.dao.DeclareStationDao;
import com.jiantong.user.impl.dao.DepartmentDao;
import com.jiantong.user.impl.dao.GovernmentDao;
import com.jiantong.user.model.DeclareStation;
import com.jiantong.user.model.Department;
import com.jiantong.user.model.Government;
import com.jiantong.user.service.DepartmentWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class DepartmentWriteServiceImpl implements DepartmentWriteService {

    private final GovernmentDao governmentDao;
    private final DepartmentDao departmentDao;
    private final DeclareStationDao declareStationDao;

    @Autowired
    public DepartmentWriteServiceImpl(GovernmentDao governmentDao, DepartmentDao departmentDao, DeclareStationDao declareStationDao) {
        this.governmentDao = governmentDao;
        this.departmentDao = departmentDao;
        this.declareStationDao = declareStationDao;
    }

    @Override
    public Response<Boolean> createNormalDepartment(Department department) {
        try {
            Long governmentId = department.getGovernmentId();
            if (governmentId == null) {
                return Response.fail("government.id.is.null");
            }
            Government government = governmentDao.findById(governmentId);
            if (government == null) {
                return Response.fail("government.not.exists");
            }

            department.setType(DepartmentType.NORMAL_DEPARTMENT.value());
            department.setStatus(DepartmentStatus.NORMAL.value());
            return Response.ok(departmentDao.create(department));
        } catch (Exception e) {
            log.error("failed to create department = {}, cause : {}", department, Throwables.getStackTraceAsString(e));
            return Response.fail("create.department.failed");
        }
    }

    @Override
    public Response<Boolean> createDeclareStation(DeclareStation station) {
        try {
            station.setStatus(DepartmentStatus.NORMAL.value());
            return Response.ok(declareStationDao.create(station));
        } catch (Exception e) {
            log.error("failed to create declare station = {}, cause : {}",
                    station, Throwables.getStackTraceAsString(e));
            return Response.fail("create.department.failed");
        }
    }

    @Override
    public Response<Long> create(Department department) {
        try {
          departmentDao.create(department);
            return Response.ok(department.getId());
        } catch (Exception e) {
            log.error("failed to create department= {}, cause : {}",
                     Throwables.getStackTraceAsString(e));
            return Response.fail("create.department.failed");
        }
    }

    @Override
    public Response<Boolean> update(Department department) {
        try {

            return Response.ok(departmentDao.update(department));
        } catch (Exception e) {
            log.error("failed to update department= {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("update.department.failed");
        }
    }
}
